package com.blacksheep.mtm.service.Impl;

import cn.hutool.http.server.HttpServerResponse;
import com.alibaba.excel.EasyExcel;
import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.common.utils.FileUtil;
import com.blacksheep.mtm.dao.TbMedicineMapper;
import com.blacksheep.mtm.dao.TbMemberMapper;
import com.blacksheep.mtm.dao.TbSupplierMapper;
import com.blacksheep.mtm.dto.MedicineItemDto;
import com.blacksheep.mtm.dto.OrderExcelDto;
import com.blacksheep.mtm.pojo.DemoData;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.*;
import com.blacksheep.mtm.search.message.DemoDataListener;
import com.blacksheep.mtm.service.MedicineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service
public class MedicineServiceImpl implements MedicineService {

    @Autowired
    private TbMedicineMapper medicineMapper;


    @Override
    public DataTablesResult getList(int draw, int start, int length, int[] state, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();
        //分页
        PageHelper.startPage(start/length+1,length);
        List<TbMedicine> list = medicineMapper.selectList("%"+search+"%",state,minDate,maxDate,orderCol,orderDir);
        PageInfo<TbMedicine> pageInfo=new PageInfo<>(list);

//        EasyExcel.read("d:/1.xls",DemoData.class,new DemoDataListener(medicineMapper)).build().readAll();
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);

        return  result;
    }

    @Override
    public int updateMedicine(TbMedicine medicine) {
        int result = medicineMapper.updateByPrimaryKeySelective(medicine);
        if (result<1){
            throw new XmallException("修改失败");
        }
        return result;
    }

    @Override
    @Transactional
    public int deleteBatch(Integer[] ids) {
        TbMedicineExample example = new TbMedicineExample();
        example.createCriteria().andMedicineIdIn(Arrays.asList(ids));
        int result = medicineMapper.deleteByExample(example);
        if (result<1){
            throw new XmallException("删除失败");
        }
        return 0;
    }


    @Override
    public TbMedicine insertMedicine(TbMedicine medicine) {
        int result = medicineMapper.insertSelective(medicine);
        if (result<1){
            throw new XmallException("添加失败");
        }
        return medicine;
    }

    public TbMedicine alertMedicineState(Integer id,Integer state){
        int result = medicineMapper.updateStateByPrimaryKey(id,state);
        if (result<1){
            throw new XmallException("修改失败");
        }
        return null;
    }

    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        int[] state = {40};
        List<TbMedicine> medicineList = medicineMapper.selectList("%%",null,null,null,"medicine_id","desc");
        for (TbMedicine medicine:medicineList){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ID",medicine.getMedicineId());
            map.put("药名",medicine.getMedicineName());
            map.put("商品名",medicine.getName());
            map.put("规格",medicine.getStandard());
            map.put("厂家",medicine.getManufacturer());
            map.put("单价",medicine.getPrice());
            map.put("是否冷藏",medicine.getFreezing());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional
    public void insertFromExcel(List<Object> sheetData1) {
        for (int i =1;i<sheetData1.size();i++){
            List<String> a =  (List<String>) sheetData1.get(i);
            Integer id = Integer.parseInt(a.get(0));
            String medicineName = a.get(1);
            String name = a.get(2);
            String standard = a.get(3);
            String factoryName = a.get(4);
            Float price = Float.valueOf(a.get(5));
            Integer freezing = Integer.parseInt(a.get(6));
            TbMedicine medicine = new TbMedicine(id,name,medicineName,1,factoryName,standard,freezing,price);
            if (id !=null && id!=0){
                if (medicineMapper.updateByPrimaryKeySelective(medicine) <1){
                    throw new XmallException(medicineName+"更新失败");
                }
            }else {
                medicine.setMedicineId(null);
                if (medicineMapper.insertSelective(medicine) <1){
                    throw new XmallException(medicineName+"新增失败");
                }
            }

        }
    }

}
